package com.daekwang.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daekwang.domain.Result;
import com.daekwang.domain.UserVO;
import com.daekwang.service.UserService;
import com.daekwang.util.Property;

@Controller
public class UserAction {

	static Logger logger = Logger.getLogger(UserAction.class.getName());

	private UserService service;

	public void setService(UserService service) {
		this.service = service;
	}

	/*
	 * @RequestMapping(value = "userAction.test.action", method =
	 * RequestMethod.POST) public void welcome(HttpServletRequest req,
	 * HttpServletResponse res) { List list = dao.select();
	 * //System.out.println(list.size());
	 * 
	 * logger.info(list.size());
	 * 
	 * try { PrintWriter write = res.getWriter();
	 * write.print("<script>alert('111');</script>"); write.flush();
	 * write.close(); } catch (IOException e) { // TODO Auto-generated catch
	 * block e.printStackTrace(); } }
	 */

	@RequestMapping(value = "user.action", params = "method=add")
	public void addUser(HttpServletRequest req, HttpServletResponse resp,
			UserVO user) {
		int result = service.userAdd(user);

		logger.debug(user.getId());
		logger.debug(user.getPassword());
		logger.debug(user.getName());
		logger.debug(result);
	}

	// @RequestMapping(value = "index.do", method = RequestMethod.GET)
	// public ModelAndView main(DaekwangDao dao) {
	// ModelAndView mav = new ModelAndView();
	//
	// System.out.println("main.jsp");
	// mav.setViewName("redirect:/aa.do");
	// return mav;
	// }

	@RequestMapping(value = "login.action", params = "method=login")
	@ResponseBody
	public Result login(HttpServletRequest request, UserVO user) {

		// @RequestBody 하면 이상하게 안됨
		Result result = new Result();
		result.setSuccess(false);

		UserVO loginUser;

		List list = service.login(user);

		if (list.size() == 1) {
			result.setSuccess(true);

			loginUser = (UserVO) list.get(0);
			request.getSession().setAttribute("loginUser", loginUser);

		} else {
			String message = Property.getInstance().getMessage(
					"cannot_find_user");
			result.setReason(message);
		}
		return result;
	}

	@RequestMapping(value = "login.action", params = "method=check")
	@ResponseBody
	public Result sessionCheck(HttpServletRequest request) {

		Result result = new Result();
		result.setSuccess(false);

		if (request.getSession().getAttribute("loginUser") != null) {

			UserVO loginUser = (UserVO) request.getSession().getAttribute(
					"loginUser");
			result.setSuccess(true);
			List list = new ArrayList();
			list.add(loginUser);
			//result.setList(list);
		} else {
			result.setSuccess(false);
			String message = Property.getInstance().getMessage("please_login");
			result.setReason(message);
		}

		return result;
	}

	@RequestMapping(value = "login.action", params = "method=logout")
	public void logout(HttpServletRequest request) {
		request.getSession().invalidate();
	}
}
